package site.lanmushan.framework.util.file;

import cn.hutool.core.io.IORuntimeException;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;

/**
 * @author Administrator
 */
public class StreamUtil {
    public static byte[] read(InputStream inputStream) {
        return read(inputStream,true);
    }

    public static byte[] read(InputStream inputStream,Boolean close) {
        byte[] bytes = new byte[1024];
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        InputStream in = inputStream;
        try {
            int len = 0;
            while ((len = inputStream.read(bytes, 0, bytes.length)) != -1) {
                byteArrayOutputStream.write(bytes,0,len);
            }
        } catch (Exception e) {
            throw new IORuntimeException(e);
        } finally {
            if(inputStream!=null&&close)
            {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                ;
            }
        }
        return byteArrayOutputStream.toByteArray();
    }
}
